package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.service.UserService;
import entity.PageResult;
import entity.QueryPageBean;
import entity.Result;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

;import java.util.List;

/**
 * @Description:
 * @Author: yp
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;


    /**
     * 获得用户名【从SpringSecurity里面获得】
     * @return
     */
    @RequestMapping("/getUserName")
    public Result getUserName(){
        try {
            //1. 获得SecurityContext上下文对象, 获得认证对象, 获得User对象
            org.springframework.security.core.userdetails.User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult =   userService.findPage(queryPageBean);
        return pageResult;
    }

    //添加:
    @RequestMapping("/add")
    public Result add(@RequestBody com.itheima.pojo.User user, Integer[] checkitemIds) {
        try {
            userService.add(user,checkitemIds);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }


    //编辑弹框回显
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            com.itheima.pojo.User user =  userService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    //编辑弹框 角色:
    /**
     * 根据checkGroupId查询检查项的ids
     * @return
     */
    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer checkGroupId){
        try {
            List<Integer>  checkItemIds =  userService.findCheckItemIdsByCheckGroupId(checkGroupId);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody com.itheima.pojo.User checkGroup, Integer[] checkitemIds){
        try {

            userService.edit(checkGroup,checkitemIds);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    //    删除数据:
    @RequestMapping("/delete")
    public Result delete(Integer checkGroupId){
        try {
            userService.delete(checkGroupId);
            return new Result(true,"删除检查组成功...");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除检查组失败...");

        }
    }

}
